﻿@using Csla.Web.Mvc
@using ProjectTracker.Library

@model ProjectTracker.Library.ProjectEdit

@{
  ViewBag.Title = "Edit project";
}

<h2>@ViewBag.Title</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm())
{
  @Html.ValidationSummary(true)
  <fieldset>
    <legend>Project</legend>
    @Html.HiddenFor(model => model.Id)
    @Html.HiddenFor(model => model.TimeStamp)
    <div class="editor-label">
      @Html.LabelFor(model => model.Name)
    </div>
    <div class="editor-field">
      @Html.HasPermission(Csla.Rules.AuthorizationActions.WriteProperty, Model, ProjectEdit.NameProperty, 
                          Html.EditorFor(model => model.Name), 
                          Html.DisplayFor(model => model.Name))
      @Html.ValidationMessageFor(model => model.Name)
    </div>
    @if (((Csla.Security.IAuthorizeReadWrite)Model).CanWriteProperty(ProjectEdit.DescriptionProperty.Name)) {
      <div class="editor-label">
        @Html.LabelFor(model => model.Description)
      </div>
      <div class="editor-field">
        @Html.EditorFor(model => model.Description)
        @Html.ValidationMessageFor(model => model.Description)
      </div>
    }
    <div class="editor-label">
      @Html.LabelFor(model => model.Started)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Started)
      @Html.ValidationMessageFor(model => model.Started)
    </div>
    <div class="editor-label">
      @Html.LabelFor(model => model.Ended)
    </div>
    <div class="editor-field">
      @Html.EditorFor(model => model.Ended)
      @Html.ValidationMessageFor(model => model.Ended)
    </div>
    <p>
      @Html.HasPermission(Csla.Rules.AuthorizationActions.EditObject, typeof(ProjectEdit), @"<input type=""submit"" value=""Save"" />", string.Empty)
    </p>
    <fieldset>
      <legend>Resources</legend>
      <table>
        <thead>
          <tr>
            <td>
              Name
            </td>
            <td>
              Assigned
            </td>
            <td>
              Role
            </td>
          </tr>
        </thead>
        <tbody>
          @foreach (var item in Model.Resources)
          { 
            <tr>
              <td>@item.FullName
              </td>
              <td>@String.Format("{0:d}", item.Assigned)
              </td>
              <td>@item.RoleName
              </td>
            </tr>
          }
        </tbody>
      </table>
      @Html.HasPermission(Csla.Rules.AuthorizationActions.EditObject, typeof(ProjectEdit), Html.ActionLink("Edit resources", "Index", "ProjectResource", new { id = Model.Id }, null), "")
    </fieldset>
  </fieldset>
}
<div>
  @Html.ActionLink("Back to List", "Index")
</div>
